<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>
        function isEmail(email) {
            var atIndex = email.indexOf('@');
            var dotIndex = email.lastIndexOf('.');
            if (atIndex == -1 || dotIndex == -1) {// 有没有
                console.log('格式错误:没有找到@或者.');
                return false;
            }
            if (dotIndex <= atIndex + 1) { // .在@前面了
                console.log('格式错误(.在@前面了)')
                return false;
            }
            if (atIndex == 0 || dotIndex == email.length - 1) {
                console.log('格式错误(@不能再最前,点不能再最后)')
                return false;
            }
            // 判断是否有两个@
            if (atIndex != email.lastIndexOf('@')) {
                console.log('格式错误(不止一个@符号)');
                return false;
            }
            return true;
        }
        function checkEmail(str) {
            var isOk = isEmail(str);// 判断是否是邮箱地址
            console.log(str, isOk);
        }
        checkEmail('@qq.com');  // 1
        checkEmail('a@b@c.com'); // 2
        checkEmail('a@'); // 3
        checkEmail('a@.com'); // 4
        checkEmail('a.com'); // 5
        checkEmail('a.qq@com'); // 6
        checkEmail('a@qq.com'); // 7
        checkEmail('a@sina.com.cn')
    </script>
</body>

</html>